﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace SqlServerDataTableToExcel.DAL
{
    public class MySql : IDAL.ISqlFuc
    {


        /// <summary>
        /// 得到数据库列表
        /// </summary>
        /// <param name="IpAddress"></param>
        /// <param name="UserName"></param>
        /// <param name="UserPwd"></param>
        /// <returns></returns>
        public List<string> GetDataSourceList(string IpAddress, string UserName, string UserPwd)
        {
            List<string> rtn = new List<string>();
            string DataSource = "information_schema";
            DBhelper.MySqlDbhelper Dbhelper = new DBhelper.MySqlDbhelper(IpAddress, DataSource, UserName, UserPwd);
            string Sql = "show databases";
            DataTable dt = Dbhelper.GetUsrDataSource(Sql);
            foreach (DataRow dr in dt.Rows)
            {
                rtn.Add(dr["Database"].ToString());
            }
            return rtn;
        }
        public List<Model.DataTableMsg> GetTableMsg(string IPAddress, string DataSource, string UserName, string Pwd)
        {

            string sysDataSource = "information_schema";
            DBhelper.MySqlDbhelper Dbhelper = new DBhelper.MySqlDbhelper(IPAddress, sysDataSource, UserName, Pwd);

            List<Model.DataTableMsg> rtn = new List<Model.DataTableMsg>();
            string SQL = @"SELECT  TABLE_NAME ,TYPE,TABLE_COMMENT FROM (
SELECT  TABLE_NAME,'表' as TYPE,TABLE_COMMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{0}' and TABLE_TYPE='BASE TABLE'
union all
SELECT TABLE_NAME,'视图' as TYPE,'' AS TABLE_COMMENT FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_SCHEMA = '{0}' ) A 
order by TYPE,TABLE_NAME";
            SQL = string.Format(SQL, DataSource);
            DataTable tbNames = Dbhelper.GetUsrDataSource(SQL);

            SQL = @"select  TABLE_NAME,COLUMN_NAME,ORDINAL_POSITION,COLUMN_COMMENT,IS_NULLABLE,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,COLUMN_DEFAULT,COLUMN_KEY from information_schema.columns where table_schema='{0}';";
            SQL = string.Format(SQL, DataSource);
            DataTable dt = Dbhelper.GetUsrDataSource(SQL);


            for (int i = 0; i < tbNames.Rows.Count; i++)
            {
                Model.DataTableMsg table = new Model.DataTableMsg();
                table.TableName = tbNames.Rows[i]["TABLE_NAME"].ToString();
                table.TableType = tbNames.Rows[i]["TYPE"].ToString();
                table.TableID = (i + 1).ToString();
                table.TableExplain = tbNames.Rows[i]["TABLE_COMMENT"].ToString();
                DataRow[] drs = dt.Select(string.Format("TABLE_NAME = '{0}'", table.TableName));
                if (drs.Length == 0)
                {
                    continue;
                }
                for (int j = 0; j < drs.Length; j++)
                {
                    Model.TableCol tblCol = new Model.TableCol();
                    tblCol.ColName = drs[j]["COLUMN_NAME"].ToString();
                    tblCol.ColType = drs[j]["DATA_TYPE"].ToString();
                    tblCol.ColLength = drs[j]["CHARACTER_MAXIMUM_LENGTH"].ToString();
                    tblCol.decimalLength = "";
                    //tblCol.decimalLength = drs[j]["小数位数"].ToString();
                    if (drs[j]["IS_NULLABLE"].ToString() == "NO")
                    {
                        tblCol.ColNull = false;
                    }
                    else
                    {
                        tblCol.ColNull = true;
                    }
                    tblCol.ColDefault = drs[j]["COLUMN_DEFAULT"].ToString();
                    if (Convert.ToString(drs[j]["COLUMN_KEY"]) == "PRI")
                    {
                        tblCol.ISKey = true;
                    }
                    else
                    {
                        tblCol.ISKey = false;
                    }
                    tblCol.ColExp = drs[j]["COLUMN_COMMENT"].ToString();
                    table.TableCols.Add(tblCol);
                    if (tblCol.ISKey)
                    {
                        if (!string.IsNullOrWhiteSpace(table.KeyName))
                        {
                            table.KeyName += ";";
                        }
                        table.KeyName += tblCol.ColName;
                    }

                }
                rtn.Add(table);
            }

            return rtn;
        }

    }
}
